package com.livery.app.model.tables;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

import com.livery.data.Car;
import com.livery.data.Client;

public class CarTableModel extends AbstractTableModel{
	
	private List<Car> carList;
	private ArrayList<TableModelListener> listeners = new ArrayList<TableModelListener>();
	private final String[] columnHeaders= {"Marka","Model","Data Produkcji","Kolor","Pojemność Silnika",
			"Przebieg","Cena za dobe","Kaucja","Dostepność"};
	
	public CarTableModel(List<Car> carList){
		this.carList = carList;
	}
	
	public CarTableModel(){
		this.carList = new ArrayList<Car>() ;
	}
	
	
	
	public List<Car> getCarList() {
		return carList;
	}

	public void setCarList(List<Car> carList) {
		this.carList = carList;
	}

	@Override
	public int getColumnCount() {
		return 9;
	}

	@Override
	public int getRowCount() {
		return carList.size();
	}
	public Car getCarAt(int row){
		return carList.get(row);
	}
	@Override
	public Object getValueAt(int r, int c) {
		switch(c)
		{
		case 0 : return carList.get(r).getMake();
		case 1 : return carList.get(r).getModel();
		case 2 : 
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			Timestamp date = (Timestamp) carList.get(r).getProductionYear();
			String data = formatter.format(date);
			return data;
		case 3 : return carList.get(r).getColor();
		case 4 : return Integer.toString(carList.get(r).getEngineDisplacement());
		case 5 : return Integer.toString(carList.get(r).getMileage());
		case 6 : return carList.get(r).getPrice().toString();
		case 7 : return Integer.toString(carList.get(r).getDeposit());
		case 8 : return carList.get(r).isAvailable();
		default : return null;
		}
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		switch(columnIndex)
		{
		case 0 : return String.class;
		case 1 : return String.class;
		case 2 : return	String.class;
		case 3 : return String.class;
		case 4 : return Integer.class;
		case 5 : return Integer.class;
		case 6 : return String.class;
		case 7 : return Integer.class;
		case 8 : return Boolean.class;
		default : return null;
		}
	}
	
	public void addRow(Car car){
		carList.add(car);
		TableModelEvent event = new TableModelEvent(this,carList.size()-1,carList.size()-1,TableModelEvent.ALL_COLUMNS,TableModelEvent.INSERT);
		for(TableModelListener tml : listeners){
			tml.tableChanged(event);
		}
	}
	
	public void updateRow(int index, Car car){
		
		carList.set(index, car);
		TableModelEvent event = new TableModelEvent( this, index, index, TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE );
		for(TableModelListener tml : listeners){
			tml.tableChanged(event);
		}
	}
	
	public void removeRow(Car car){
		int index = carList.indexOf(car);
		carList.remove(index);
		
		TableModelEvent event = new TableModelEvent( this, index, index, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE );
		for(TableModelListener tml : listeners){
			tml.tableChanged(event);
		}
		
	}
	
	@Override
	public String getColumnName(int column) {
		
		return columnHeaders[column];
	}
	
	@Override
	public void addTableModelListener(TableModelListener l) {
		if(listeners.contains(l)){
			return; 
		}
		
		listeners.add(l);
	}
	
	@Override
	public void removeTableModelListener(TableModelListener l) {
		listeners.remove(l);
	}

}
